package com.sky.controller.admin;

import com.sky.constant.ShopStatusConstant;
import com.sky.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController("adminShopController")  //如果不设置会跟user/ShopController在IOC中重名,因为类名一样
@RequestMapping("/admin/shop")
@Api(tags = "管理端店铺控制器")
@Slf4j
public class ShopController {

    @Resource
    private RedisTemplate redisTemplate;

    /**
     * 设置营业状态
     *
     * @param status
     * @return
     */
    @PutMapping("/{status}")
    @ApiOperation("设置营业状态")
    public Result<String> setStatus(@PathVariable Integer status){
        log.info("[修改营业状态] 修改营业状态为: {}", status.equals(ShopStatusConstant.OPEN) ?"营业中":"暂停营业");
        redisTemplate.opsForValue().set("SHOP_STATUS",status);
        log.info("[修改营业状态] 修改完毕");
        return Result.success();
    }

    @ApiOperation("获取营业状态")
    @GetMapping("/status")
    public Result<Integer> getStatus(){
        log.info("[管理端获取营业状态] 开始读取redis");
        Object shop_status = redisTemplate.opsForValue().get(ShopStatusConstant.SHOP_STATUS);
        if(shop_status!=null){
            log.info("[管理端获取营业状态] 获取成功");
            return Result.success((Integer) shop_status);
        }else{
            log.info("[管理端获取营业状态] 获取失败");
            return Result.error("获取营业状态失败");
        }
    }
}
